package org.fioma.tree.bytes;

import org.fioma.general.Tools;
import org.fioma.tree.LabelRef;

public class LabelRefBytes extends LabelRef<LabelRefBytes, byte[]> {

	byte[] word;

	public LabelRefBytes(byte[] word, int start, int length, LabelRefBytesFactory labelFactory) {
		super(word, start, length, labelFactory);
	}

	public LabelRefBytes(byte[] word, LabelRefBytesFactory labelFactory) {
		super(word, 0, word[0], labelFactory);
	}

	@Override
	public byte getByte(int pos) {
		if (pos < 0 || pos >= getLength()) {
			throw new RuntimeException("Position " + pos + " invalid in the word with length " + getLength());
		}
		return Tools.getByte(getWord(), getStart() + pos);
	}

	public int getMinsmatch(LabelRefBytes word) {
		return Tools.getMismatch(getWord(), getStart(), getLength(), word.getWord(), word.getStart(), word.getLength());
	}

	@Override
	public int getWordLength() {
		if (word == null || word.length == 0) {
			return 0;
		} else {
			return word[0];
		}
	}

	@Override
	public byte[] getBytes() {
		return getWord();
	}

}
